/*****************************************************************************
* ui_sys_footer.c: Copyright 2023 (c) CoreLink
* Desc:           
* 
* Author: Jeanne
* Date: 2023-09-07 11:37:57
* Notes: 
* 
* ---------------------------------------------------------------------------
* LastEditors: 
* LastEditTime: 
*****************************************************************************/
/*------------------------------- Includes ----------------------------------*/
#include"ui_sys_footer.h"


/*------------------- 全局定义和声明 -------------------*/

/*---------------------- 常量/宏定义 -----------------------*/
#define ITEM_SIZE ((10 * MY_UI_H_ZOOM) > 2 ? (10 * MY_UI_H_ZOOM) : 2)
#define ITEM_SPACE ((6 * MY_UI_W_ZOOM) > 1 ? (6 * MY_UI_W_ZOOM) : 1)
#define OFF_COLOR (0x9f9f9f)
#define ON_COLOR (0xab03f2)
//回调上报间隔3* lvgl 长按间隔时间（目前为100ms） = 300ms
#define CB_REPORTED_TIME 3

/*----------------------- 类型声明 ---------------------------------*/

/*----------------------- 变量声明 -----------------------------*/

/*----------------------- 函数原型 --------------------------------*/
static void footer_event_cb(lv_event_t *e);
static int get_index_by_x(ui_sys_footer_info_t *info,int x);

/*----------------------- 功能实现 --------------------------------*/

ui_sys_footer_info_t *ui_sys_footer_create(lv_obj_t *parent, uint8_t size, footer_event_cb_t footer_cb, void *cb_arg)
{
    ui_sys_footer_info_t *info;
    uint16_t info_size = 0;

    info_size = sizeof(ui_sys_footer_info_t) + sizeof(lv_obj_t *)*size;

    PR_DEBUG("footer_info size = %d, item_list_size = %d, total size = %d \n ",sizeof(ui_sys_footer_info_t),sizeof(lv_obj_t *)*size,info_size);

    //alloc 
    info = (ui_sys_footer_info_t *)MY_MALLOC(info_size);

    memset(info, 0, info_size);

    info->parent = parent;
    info->item_num = size;
    info->footer_cb = footer_cb;
    info->cb_arg = cb_arg;

    PR_DEBUG("ui_sys_footer_create \n ");

    if(info)
    {
       PR_DEBUG("create ui_sys_footer_create successful \n ");
    }
    return info;
}
int ui_sys_footer_show(ui_sys_footer_info_t *info)
{
    if (!info)
    {
        return -1;
    }

    info->old_index = -1;
    info->cont = lv_obj_create(info->parent);
    lv_obj_align(info->cont, LV_ALIGN_BOTTOM_MID, 0, -50 * MY_UI_H_ZOOM);
    lv_obj_set_size(info->cont, (ITEM_SPACE * 2 + ITEM_SIZE) * info->item_num, (ITEM_SPACE + ITEM_SIZE) * 2);
    lv_obj_set_style_pad_all(info->cont, 0, 0);
    lv_obj_set_style_border_width(info->cont, 0, 0);
    lv_obj_set_style_radius(info->cont, 0, 0);
    lv_obj_set_style_bg_opa(info->cont, LV_OPA_0, 0);

    for (int i = 0; i < info->item_num; i++)
    {
        lv_obj_t *obj = lv_led_create(info->cont);
        lv_obj_set_size(obj, ITEM_SIZE, ITEM_SIZE);

        lv_obj_set_style_shadow_spread(obj, 5 * MY_UI_W_ZOOM, 0);
        lv_led_off(obj);
        int x = ITEM_SPACE + i * (ITEM_SIZE + ITEM_SPACE * 2);
        int y = ITEM_SPACE;
        lv_obj_set_pos(obj, x, y);
        info->item_list[i] = obj;
        lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE);
    }
    ui_sys_footer_select(info, 0);

    lv_obj_add_event_cb(info->cont,footer_event_cb, LV_EVENT_CLICKED, info);
    lv_obj_add_event_cb(info->cont,footer_event_cb, LV_EVENT_PRESSING, info);    

    return 0;

}
int ui_sys_footer_delete(ui_sys_footer_info_t *info)
{
    if (!info)
    {
        return -1;
    }

    if (info->cont)
    {
        //删除obj本身以及其子对象
        lv_obj_del(info->cont);
    }
    MY_FREE(info);
    return 0;
}

int ui_sys_footer_select(ui_sys_footer_info_t *info, int index)
{
    if (!info)
    {
        return -1;
    }

    if (index >= info->item_num)
    {
        return -1;
    }

    if (info->old_index != -1)
    {
        lv_led_off(info->item_list[info->old_index]);
    }
    info->old_index = index;
    lv_led_on(info->item_list[info->old_index]);

    return 0;

}
int ui_sys_footer_set_bg_color(ui_sys_footer_info_t *info, lv_color_t color)
{
    if (!info)
    {
        return -1;
    }

    for (int i = 0; i < info->item_num; i++)
    {
        lv_obj_set_style_bg_color(info->item_list[i], color, 0);
    }

    return 0;
}

static void footer_event_cb(lv_event_t *e)
{
    ui_sys_footer_info_t *_this = (ui_sys_footer_info_t *)lv_event_get_user_data(e);
    lv_event_code_t code = lv_event_get_code(e);
    if (code != LV_EVENT_CLICKED && code != LV_EVENT_PRESSING)
    {
        return;
    }

    if (!_this || !_this->footer_cb)
    {
        return;
    }

    lv_indev_t *indev = lv_indev_get_act();
    if (indev == NULL)
        return;

    lv_indev_type_t indev_type = lv_indev_get_type(indev);
    lv_point_t p;
    if (indev_type != LV_INDEV_TYPE_POINTER)
    {
        return;
    }
    lv_indev_get_point(indev, &p);
    // LV_LOG_USER("%p x  %d  y  %d", obj, p.x, p.y);
    int index = get_index_by_x(_this,p.x);
    if (index == -1)
    {
        return;
    }

    // footer的user_data用于记录cb进入次数，控制回调间隔
    int *count = (int *)&_this->cont->user_data;
    if (code == LV_EVENT_CLICKED)
    {
        *count = 0;
    }

    if (*count == 0 || *count > CB_REPORTED_TIME)
    {
        _this->footer_cb(index, _this->cb_arg);
        *count = 0;
    }
    (*count)++;

    ui_sys_footer_select(_this,index);
}
static int get_index_by_x(ui_sys_footer_info_t *info,int x)
{
    if (!info)
    {
        return -1;
    }

    int parent_x = lv_obj_get_x(info->cont);
    int item_size = ITEM_SIZE + ITEM_SPACE * 2;
    if (x <= parent_x)
    {
        return 0;
    }
    int diff = x - parent_x;
    int index = diff / item_size;

    if(info->item_num == 0)
    {
        return 0;
    }

    if (index >= info->item_num - 1)
    {
        index = info->item_num - 1;
    }
    return index;
}

/*---------------------------------------------------------------------------*/
